<?php

namespace App\Http\Controllers\Admin;

use App\Models\Orm\DataDeliberyCount;
use App\Models\Orm\Delivery;
use Illuminate\Http\Request;


/**
 * 渠道
 * Class DeliveryController
 * @package App\Http\Controllers\Admin
 */
class DeliveryController extends BaseController
{
    /**
     * 渠道列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        // 当前用户
        $user = auth('admin')->user();

        // 三方运营,显示自己的渠道
        if ($user->isOperator()) {

            $data = Delivery::where('admin_user_id', $user->id)->orderBy('id', 'desc')->get();

            return view('admin.delivery.index', compact('data'));
        }

        // 管理,显示所有渠道
        $data = Delivery::all();

        return view('admin.delivery.index', compact('data'));
    }

    /**
     * 渠道详情
     * @param $id
     * @return $this|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function detail($id)
    {
        // 当前用户
        $user = auth('admin')->user();

        $delivery = Delivery::find($id);

        if (!$delivery) {
            return back()->withErrors('渠道不存在');
        }

        if ($user->isOperator() && $delivery->admin_user_id != $user->id) {
            return back()->withErrors('您无权查看该渠道');
        }

        $data = DataDeliberyCount::where('channel_id', $delivery->id)->get();
//        dd($data);
        return view('admin.delivery.detail', compact('data', 'delivery'));
    }


}

